package com.train.ticket.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.train.dto.TicketDTO;
import com.train.resp.TicketPageResp;
import com.train.common.exception.GlobalException;
import com.train.common.enums.impl.BizExceptionEnums;
import com.train.common.resp.PageResp;
import com.train.dto.TicketPageDTO;
import com.train.ticket.mapper.TicketMapper;
import com.train.ticket.po.TicketPO;
import com.train.ticket.service.TicketService;
import lombok.RequiredArgsConstructor;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【ticket(车票)】的数据库操作Service实现
 * @createDate 2025-03-14 15:17:16
 */
@Service
@RequiredArgsConstructor
public class TicketServiceImpl extends ServiceImpl<TicketMapper, TicketPO>
    implements TicketService {

    private final TicketMapper ticketMapper;

    @Override
    public PageResp<TicketPageResp> pageList(TicketPageDTO ticketPageDTO) {
        return ticketMapper.pageList(ticketPageDTO);
    }

    @Override
    public void remove(Long id) {
        boolean isRemoved = this.removeById(id);
        if (!isRemoved) {
            throw new GlobalException(BizExceptionEnums.EXP_REMOVE_TICKET);
        }
    }

    @Override
    public void batchSaveTicket(List<TicketDTO> dto) {
        TicketService proxy = (TicketService) AopContext.currentProxy();
        List<TicketPO> ticketPOS = BeanUtil.copyToList(dto, TicketPO.class);
        boolean isSaved = proxy.saveBatch(ticketPOS);
        if (!isSaved) {
            throw new GlobalException(BizExceptionEnums.EXP_ADD_TICKET);
        }
    }
}




